{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 64,
   "id": "d02ca9ae-4364-4e3c-88fb-056b77d49f13",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "id": "df6e5440-fd69-4f05-93e7-278f1f414948",
   "metadata": {},
   "outputs": [],
   "source": [
    "x=2*np.random.rand(100,1)\n",
    "y=4+3*x+np.random.randn(100,1)\n",
    "x_b=np.c_[np.ones((100,1)),x]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "id": "6fdf461f-9800-46a4-b056-75a06cd74e3f",
   "metadata": {},
   "outputs": [],
   "source": [
    "n_epochs=10000\n",
    "n_batchs=100"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a79faa18-715f-411b-a9bb-f79a8360effe",
   "metadata": {},
   "source": [
    "随机梯度下降，这里选100个批次，每个批次随机选一组数据进行梯度计算  \n",
    "wj t+1=wj-n*gj  \n",
    "这里和全量类似，进行向量计算，不过只用一个数据计算  \n",
    "双重for，内层随机一个向量下标索引    \n",
    "从x_b  y矩阵中取出第i行，即一个方程，总的就100行2列，咋可能取第i列\n",
    "2行1列  1行2列 2行1列 1行1列   结果2行1列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "id": "ca08764c-f246-4761-8e89-9da38ad41d65",
   "metadata": {},
   "outputs": [],
   "source": [
    "theta=np.random.randn(2,1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "id": "e7fbea49-451b-421f-911f-e41999177449",
   "metadata": {},
   "outputs": [],
   "source": [
    "t0,t1=1,200\n",
    "def learning_rate_adjust(t):\n",
    "    return t0/(t+t1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "id": "6ecc01a7-28e4-4e91-b452-0d91d4084101",
   "metadata": {},
   "outputs": [],
   "source": [
    "for t in range(n_epochs):\n",
    "    s_index=np.random.permutation(100)\n",
    "    s_x_b=x_b[s_index]\n",
    "    s_y=y[s_index]\n",
    "    for i in range(n_batchs):\n",
    "        xi=s_x_b[i:i+1]\n",
    "        yi=s_y[i:i+1]\n",
    "        gradients=xi.T.dot(xi.dot(theta)-yi)\n",
    "        learning_rate=learning_rate_adjust(t+i)\n",
    "        theta=theta-learning_rate * gradients"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "id": "30877200-8d8d-47dd-af77-4606ad49e3b2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[3.83569192]\n",
      " [3.11419393]]\n"
     ]
    }
   ],
   "source": [
    "print(theta)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "60f96f17-2dec-4f44-9746-4a701ca7d3ac",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
